<?php
namespace app\admin\controller;
use think\Db;
class Emailac extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    email_ac
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (email like ? or host like ? or name like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            #分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
			$page_data   = com_page_data($total, $page,  $page_size);
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by sort desc, id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']      =  com_format_checkbox($v['id']);
                $list[$k]['create_time']   =  com_format_time( $v['create_time'] );
                $list[$k]['sort']          =  com_format_input_edit( url("ajax/editSort", 't=email_ac'),$v['id'],$v['sort'] );
                $list[$k]['status']        =  com_format_status2( url("ajax/editStatus", 't=email_ac'),$v['id'], $v['status'] );
                
                //操作按钮
                $list[$k]['action']       = com_page(['ico'=>'edit','title'=>'编辑账号', 'url'=>url('emailac/edit','id='.$v['id'])]);
                $list[$k]['action']       .= com_page(['ico'=>'del3','title'=>'删除','url'=>url('c/del'), 'post'=>['id'=>$v['id'],'t'=>'email_ac'],'confirm'=>"你确定要删除[{$v['email']}]吗"]);
            }
			$com_body    = com_build_table($list, array("checkbox","id","host", "port","email","name", "return_email","return_name","sort","status" ,"create_time", "action"));
			$com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        } 
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['邮件账号', '列表'],
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $d = input();
            $rule = [
                   'host'      =>  'require',
                   'email'       =>  'require',
                   'password'      =>  'require',
                ];
            $msg = [
                   'host.require'      =>  '发送主机不能为空',
                   'email.require'      =>  '发送email账号不能为空',
                   'password.require'      =>  '发送账号的密码不能为空',
                ];
            com_validate( $rule, $msg, $d, true );
            $d['create_time'] = time();
            Db::name('email_ac')->insert($d);
            $config = [
                'suc' => [
                    'msg' => '添加账号成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        $d = [
            'status'    => c('type.status'),
        ];
        return view('', $d);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $d = input();
            $rule = [
                   'host'      =>  'require',
                   'email'       =>  'require',
                   'password'      =>  'require',
                ];
            $msg = [
                   'host.require'      =>  '发送主机不能为空',
                   'email.require'      =>  '发送email账号不能为空',
                   'password.require'      =>  '发送账号的密码不能为空',
                ];
            com_validate( $rule, $msg, $d, true );
            $d['id'] = $id;
            $d['update_time'] = time();
            Db::name('email_ac')->update($d);
            $config = [
                'suc' => [
                    'msg' => '编辑账号成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'info'      => Db::name('email_ac')->find($id),
        ];
        return view('', $data);
    }
}